﻿#include "userrelationinfowidget.h"
#include "ui_userrelationinfowidget.h"
#include <QMessageBox>

UserRelationInfoWidget::UserRelationInfoWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UserRelationInfoWidget)
{
    ui->setupUi(this);

    setTabOrder(ui->lineEditURUsername, ui->lineEditURCompanyId);
    setTabOrder(ui->lineEditURCompanyId, ui->lineEditURClientName);
    setTabOrder(ui->lineEditURClientName, ui->lineEditURContact);
    setTabOrder(ui->lineEditURContact, ui->lineEditURTel);
    setTabOrder(ui->lineEditURTel, ui->lineEditURRemark);
    setTabOrder(ui->lineEditURRemark, ui->lineEditURCredit);
    setTabOrder(ui->lineEditURCredit, ui->lineEditURIsPlatform);
    setTabOrder(ui->lineEditURIsPlatform, ui->lineEditURTicketOut);
    setTabOrder(ui->lineEditURTicketOut, ui->lineEditURMoneyIn);
    setTabOrder(ui->lineEditURMoneyIn, ui->lineEditURPreTicketOut);
    setTabOrder(ui->lineEditURPreTicketOut, ui->lineEditURPreTicketIn);
    setTabOrder(ui->lineEditURPreTicketIn, ui->lineEditURPreMoneyIn);
    setTabOrder(ui->lineEditURPreMoneyIn, ui->lineEditURPreMoneyOut);
    setTabOrder(ui->lineEditURPreMoneyOut, ui->lineEditUserNickName);
    setTabOrder(ui->lineEditUserNickName, ui->lineEditUserPosition);
    setTabOrder(ui->lineEditUserPosition, ui->lineEditUserTel);
    setTabOrder(ui->lineEditUserTel, ui->lineEditUserMobile);
    setTabOrder(ui->lineEditUserMobile, ui->lineEditUserQQ);
    setTabOrder(ui->lineEditUserQQ, ui->lineEditUserWechat);
    setTabOrder(ui->lineEditUserWechat, ui->lineEditCompanyName);
    setTabOrder(ui->lineEditCompanyName, ui->lineEditContact);
    setTabOrder(ui->lineEditContact, ui->lineEditCompanyTel);
    setTabOrder(ui->lineEditCompanyTel, ui->lineEditFax);
    setTabOrder(ui->lineEditFax, ui->lineEditWebsite);
    setTabOrder(ui->lineEditWebsite, ui->lineEditEmail);
    setTabOrder(ui->lineEditEmail, ui->lineEditProvince);
    setTabOrder(ui->lineEditProvince, ui->lineEditCity);
    setTabOrder(ui->lineEditCity, ui->lineEditDistrict);
    setTabOrder(ui->lineEditDistrict, ui->lineEditAddress);
    setTabOrder(ui->lineEditAddress, ui->lineEditReceiptAccount);
    setTabOrder(ui->lineEditReceiptAccount, ui->lineEditIntroduction);
    setTabOrder(ui->lineEditIntroduction, ui->lineEditTicketCompanyName);
    setTabOrder(ui->lineEditTicketCompanyName, ui->lineEditTaxNum);
    setTabOrder(ui->lineEditTaxNum, ui->lineEditBank);
    setTabOrder(ui->lineEditBank, ui->lineEditTicketAccount);
    setTabOrder(ui->lineEditTicketAccount, ui->lineEditTicketTel);
    setTabOrder(ui->lineEditTicketTel, ui->lineEditTicketAddress);
    setTabOrder(ui->lineEditTicketAddress, ui->pushButtonSave);
    setTabOrder(ui->pushButtonSave, ui->pushButtonCancel);
    setTabOrder(ui->lineEditReceiptAccount1, ui->lineEditReceiptAccount2);
    setTabOrder(ui->lineEditReceiptAccount2, ui->lineEditReceiptAccount3);
    setTabOrder(ui->lineEditReceiptAccount3, ui->lineEditReceiptAccount4);

    connect(ui->labelBtnAdd, &ClickedLabel::clicked,
            [=]()
    {
        if(!ui->lineEditReceiptAccount->text().isEmpty() && ui->lineEditReceiptAccount1->isHidden()){
            ui->lineEditReceiptAccount1->show();
            return;
        }

        if(!ui->lineEditReceiptAccount1->text().isEmpty() && ui->lineEditReceiptAccount2->isHidden()){
            ui->lineEditReceiptAccount2->show();
            return;
        }

        if(!ui->lineEditReceiptAccount2->text().isEmpty() && ui->lineEditReceiptAccount3->isHidden()){
            ui->lineEditReceiptAccount3->show();
            return;
        }

        if(!ui->lineEditReceiptAccount3->text().isEmpty() && ui->lineEditReceiptAccount4->isHidden()){
            ui->lineEditReceiptAccount4->show();
            return;
        }
    });

    connect(&userRelationService, &UserRelationService::queryUserRelationDetailSuccess,
            [=](User &user, Company &company)
    {
        ui->lineEditUserNickName->setText(user.nickName);
        ui->lineEditUserPosition->setText(user.position);
        ui->lineEditUserTel->setText(user.tel);
        ui->lineEditUserMobile->setText(user.mobile);
        ui->lineEditUserQQ->setText(user.mobile);
        ui->lineEditUserWechat->setText(user.wechat);

        ui->lineEditCompanyName->setText(company.companyName);
        ui->lineEditContact->setText(company.contact);
        ui->lineEditCompanyTel->setText(company.tel);
        ui->lineEditFax->setText(company.fax);
        ui->lineEditWebsite->setText(company.website);
        ui->lineEditEmail->setText(company.email);
        ui->lineEditProvince->setText(company.province);
        ui->lineEditCity->setText(company.city);
        ui->lineEditDistrict->setText(company.district);
        ui->lineEditAddress->setText(company.address);

        QString account = company.account;
        QStringList accounts = account.split("#");

        if(accounts.size() > 0){
            ui->lineEditReceiptAccount->setText(accounts[0]);
        }
        if(accounts.size() > 1){
            ui->lineEditReceiptAccount1->show();
            ui->lineEditReceiptAccount1->setText(accounts[1]);
        }
        if(accounts.size() > 2){
            ui->lineEditReceiptAccount2->show();
            ui->lineEditReceiptAccount2->setText(accounts[2]);
        }
        if(accounts.size() > 3){
            ui->lineEditReceiptAccount3->show();
            ui->lineEditReceiptAccount3->setText(accounts[3]);
        }
        if(accounts.size() > 4){
            ui->lineEditReceiptAccount4->show();
            ui->lineEditReceiptAccount4->setText(accounts[4]);
        }

        ui->lineEditIntroduction->setText(company.introduction);
        ui->lineEditTicketCompanyName->setText(company.ticketCompanyName);
        ui->lineEditTaxNum->setText(company.taxNum);
        ui->lineEditBank->setText(company.bank);
        ui->lineEditTicketAccount->setText(company.ticketAccount);
        ui->lineEditTicketTel->setText(company.ticketTel);
        ui->lineEditTicketAddress->setText(company.ticketAddress);

        ui->pushButtonSave->setEnabled(true);
    });

    connect(&userRelationService, &UserRelationService::buildSubAccountSuccess,
            [=]()
    {
        ui->labelURUsername->setText("子账号");
        ui->pushButtonBuildSubAccount->setText("已建立子账号");
        ui->pushButtonBuildSubAccount->setEnabled(false);
    });

    connect(&userRelationService, &UserRelationService::changeUserRelationSuccess,
            [=]()
    {
        ui->pushButtonChangeRelation->setText("客/供关系");
        ui->pushButtonChangeRelation->setEnabled(false);
    });
}

UserRelationInfoWidget::~UserRelationInfoWidget()
{
    delete ui;
}

void UserRelationInfoWidget::setWidgetType(UserRelation::RelationType widgetType)
{
    this->widgetType = widgetType;

    if(widgetType == UserRelation::Client){
        setWindowTitle("客户信息");
        ui->labelURCredit->setVisible(true);
        ui->lineEditURCredit->setVisible(true);
    }else{
        setWindowTitle("供应商信息");
        ui->labelURCredit->setVisible(false);
        ui->lineEditURCredit->setVisible(false);
    }
}

void UserRelationInfoWidget::setDirectory(QStringList primaryDirectoryList, QList<QStringList> secondaryDirectoryList)
{
    this->primaryDirectoryList = primaryDirectoryList;
    this->secondaryDirectoryList = secondaryDirectoryList;
    ui->comboBoxPrimaryDirectory->clear();
    ui->comboBoxPrimaryDirectory->addItems(primaryDirectoryList);
}

void UserRelationInfoWidget::on_comboBoxPrimaryDirectory_currentIndexChanged(int index)
{
    if(index != -1){
        ui->comboBoxSecondaryDirectory->clear();
        ui->comboBoxSecondaryDirectory->addItems(this->secondaryDirectoryList[index]);
    }
}

void UserRelationInfoWidget::initWithUserRelation(UserRelation *relation)
{
    this->relation = relation;
    ui->lineEditURUsername->setText(relation->targetUsername);
    ui->lineEditURCompanyId->setText(relation->targetCompanyId);
    ui->lineEditURClientName->setText(relation->nickName);
    ui->lineEditURContact->setText(relation->contact);
    ui->lineEditURTel->setText(relation->tel);
    ui->lineEditURRemark->setText(relation->remark);
    ui->lineEditURCredit->setText(QString("%1").arg(relation->credit));
    ui->lineEditURIsPlatform->setText(relation->isPlatform?"是":"否");
    if(relation->type == UserRelation::Client){
        ui->labelURTicketOut->setText("应开发票");
        ui->labelURMoneyIn->setText("应收货款");
        ui->lineEditURTicketOut->setText(QString("%1").arg(relation->ticketOut));
        ui->lineEditURMoneyIn->setText(QString("%1").arg(relation->moneyIn));
    }else{
        ui->labelURTicketOut->setText("应收发票");
        ui->labelURMoneyIn->setText("应付货款");
        ui->lineEditURTicketOut->setText(QString("%1").arg(relation->ticketIn));
        ui->lineEditURMoneyIn->setText(QString("%1").arg(relation->moneyOut));
    }
    ui->lineEditURPreTicketIn->setText(QString("%1").arg(relation->preTicketIn));
    ui->lineEditURPreTicketOut->setText(QString("%1").arg(relation->preTicketOut));
    ui->lineEditURPreMoneyIn->setText(QString("%1").arg(relation->preMoneyIn));
    ui->lineEditURPreMoneyOut->setText(QString("%1").arg(relation->preMoneyOut));
    ui->comboBoxPrimaryDirectory->setCurrentText(relation->primaryDirectory);
    ui->comboBoxSecondaryDirectory->setCurrentText(relation->secondaryDirectory);
    ui->pushButtonBuildSubAccount->setEnabled(!relation->isPlatform);
    ui->pushButtonBuildSubAccount->setText(relation->isPlatform?"无需子账号":"建立子账号");
    ui->pushButtonChangeRelation->setText(relation->type==UserRelation::Client?"添加为供应商":"添加为客户");

    if(relation->hasSubAccount)
    {
        ui->labelURUsername->setText("子账号");
        ui->pushButtonBuildSubAccount->setText("已建立子账号");
        ui->pushButtonBuildSubAccount->setEnabled(false);
    }else{
        ui->labelURUsername->setText("个人ID");
    }

    if(relation->type > 2){
        ui->pushButtonChangeRelation->setText("客/供关系");
        ui->pushButtonChangeRelation->setEnabled(false);
    }else{
        ui->pushButtonChangeRelation->setEnabled(true);
    }

    bool readOnly = relation->isPlatform;

    ui->lineEditURClientName->setReadOnly(readOnly);
    ui->lineEditURContact->setReadOnly(readOnly);
    ui->lineEditURTel->setReadOnly(readOnly);

    ui->lineEditUserNickName->setReadOnly(readOnly);
    ui->lineEditUserPosition->setReadOnly(readOnly);
    ui->lineEditUserTel->setReadOnly(readOnly);
    ui->lineEditUserMobile->setReadOnly(readOnly);
    ui->lineEditUserQQ->setReadOnly(readOnly);
    ui->lineEditUserWechat->setReadOnly(readOnly);

    ui->lineEditCompanyName->setReadOnly(readOnly);
    ui->lineEditContact->setReadOnly(readOnly);
    ui->lineEditCompanyTel->setReadOnly(readOnly);
    ui->lineEditFax->setReadOnly(readOnly);
    ui->lineEditWebsite->setReadOnly(readOnly);
    ui->lineEditEmail->setReadOnly(readOnly);
    ui->lineEditProvince->setReadOnly(readOnly);
    ui->lineEditCity->setReadOnly(readOnly);
    ui->lineEditDistrict->setReadOnly(readOnly);
    ui->lineEditAddress->setReadOnly(readOnly);
    ui->lineEditReceiptAccount->setReadOnly(readOnly);
    ui->lineEditReceiptAccount1->setReadOnly(readOnly);
    ui->lineEditReceiptAccount2->setReadOnly(readOnly);
    ui->lineEditReceiptAccount3->setReadOnly(readOnly);
    ui->lineEditReceiptAccount4->setReadOnly(readOnly);
    ui->lineEditIntroduction->setReadOnly(readOnly);
    ui->lineEditTicketCompanyName->setReadOnly(readOnly);
    ui->lineEditTaxNum->setReadOnly(readOnly);
    ui->lineEditBank->setReadOnly(readOnly);
    ui->lineEditTicketAccount->setReadOnly(readOnly);
    ui->lineEditTicketTel->setReadOnly(readOnly);
    ui->lineEditTicketAddress->setReadOnly(readOnly);
    ui->lineEditReceiptAccount->clear();
    ui->lineEditReceiptAccount1->clear();
    ui->lineEditReceiptAccount1->hide();
    ui->lineEditReceiptAccount2->clear();
    ui->lineEditReceiptAccount2->hide();
    ui->lineEditReceiptAccount3->clear();
    ui->lineEditReceiptAccount3->hide();
    ui->lineEditReceiptAccount4->clear();
    ui->lineEditReceiptAccount4->hide();

    ui->labelBtnAdd->setHidden(readOnly);
    ui->pushButtonSave->setEnabled(false);

    if(relation->isPlatform ||
            relation->ticketIn!=0.0 ||
            relation->ticketOut!=0.0 ||
            relation->moneyIn!=0.0 ||
            relation->moneyOut!=0.0 ||
            relation->preTicketIn!=0.0 ||
            relation->preTicketOut!=0.0 ||
            relation->preMoneyIn!=0.0 ||
            relation->preMoneyOut!=0.0){
        ui->pushButtonDelete->setVisible(false);
    }else{
        ui->pushButtonDelete->setVisible(true);
    }

    userRelationService.queryUserRelationDetail(this, relation->targetUsername, relation->targetCompanyId);
}

void UserRelationInfoWidget::on_lineEditURClientName_textChanged(const QString &text)
{
    ui->lineEditCompanyName->setText(text);
}

void UserRelationInfoWidget::on_lineEditURContact_textChanged(const QString &text)
{
    ui->lineEditUserNickName->setText(text);
}

void UserRelationInfoWidget::on_lineEditURTel_textChanged(const QString &text)
{
    ui->lineEditUserTel->setText(text);
}

void UserRelationInfoWidget::on_lineEditUserNickName_textChanged(const QString &text)
{
    ui->lineEditURContact->setText(text);
}

void UserRelationInfoWidget::on_lineEditUserTel_textChanged(const QString &text)
{
    ui->lineEditURTel->setText(text);
}

void UserRelationInfoWidget::on_lineEditCompanyName_textChanged(const QString &text)
{
    ui->lineEditURClientName->setText(text);
}

void UserRelationInfoWidget::on_pushButtonCancel_clicked()
{
    this->close();
}

void UserRelationInfoWidget::on_pushButtonBuildSubAccount_clicked()
{
    userRelationService.buildSubAccount(this, ui->lineEditURUsername->text(), widgetType);
}

void UserRelationInfoWidget::on_pushButtonChangeRelation_clicked()
{
    userRelationService.changeUserRelation(this, ui->lineEditURUsername->text(), widgetType);
}

void UserRelationInfoWidget::on_pushButtonSave_clicked()
{
    if(userService.inputOperatePassword(this) == false){
        return;
    }

    QString accounts;
    QString account0 = ui->lineEditReceiptAccount->text();

    accounts.append(account0);
    if(ui->lineEditReceiptAccount1->isVisible()){
        QString account1 = ui->lineEditReceiptAccount1->text();
        if(!account1.isEmpty()) accounts.append("#").append(account1);
    }
    if(ui->lineEditReceiptAccount2->isVisible()){
        QString account2 = ui->lineEditReceiptAccount2->text();
        if(!account2.isEmpty()) accounts.append("#").append(account2);
    }
    if(ui->lineEditReceiptAccount3->isVisible()){
        QString account3 = ui->lineEditReceiptAccount3->text();
        if(!account3.isEmpty()) accounts.append("#").append(account3);
    }
    if(ui->lineEditReceiptAccount4->isVisible()){
        QString account4 = ui->lineEditReceiptAccount4->text();
        if(!account4.isEmpty()) accounts.append("#").append(account4);
    }

    userRelationService.updateUserRelation(
                this,
                ui->lineEditURUsername->text(),
                widgetType,
                ui->lineEditURClientName->text(),
                ui->lineEditURContact->text(),
                ui->lineEditURTel->text(),
                ui->lineEditURRemark->text(),
                ui->lineEditURCredit->text(),
                ui->comboBoxPrimaryDirectory->currentText(),
                ui->comboBoxSecondaryDirectory->currentText(),
                ui->lineEditUserNickName->text(),
                ui->lineEditUserPosition->text(),
                ui->lineEditUserTel->text(),
                ui->lineEditUserMobile->text(),
                ui->lineEditUserQQ->text(),
                ui->lineEditUserWechat->text(),
                ui->lineEditCompanyName->text(),
                ui->lineEditContact->text(),
                ui->lineEditCompanyTel->text(),
                ui->lineEditFax->text(),
                ui->lineEditWebsite->text(),
                ui->lineEditEmail->text(),
                ui->lineEditProvince->text(),
                ui->lineEditCity->text(),
                ui->lineEditDistrict->text(),
                ui->lineEditAddress->text(),
                accounts,
                ui->lineEditIntroduction->text(),
                ui->lineEditTicketCompanyName->text(),
                ui->lineEditTaxNum->text(),
                ui->lineEditBank->text(),
                ui->lineEditTicketAccount->text(),
                ui->lineEditTicketTel->text(),
                ui->lineEditTicketAddress->text());
}

void UserRelationInfoWidget::on_pushButtonDelete_clicked()
{
    int res = QMessageBox::question(this, "提示", "是否删除此用户？", tr("确定"), tr("取消"));
    if(res == 0){
        if(userService.inputOperatePassword(this)){
            userRelationService.deleteUserRelation(this, relation);
        }
    }
}
